--#include "data\config\activityconfig\OpenServer\OpenServerAdvertisementPageConfig.lua"  once

--[[
请求状态数据入口
]]
function OpenServerADPageAwardStateHandle( sysarg )
	--print("OpenServerADPageAwardStateHandle .. ")
	SendADPageAwardsDrawInfo( sysarg )
end

function IsHasDrawADtPageAwards( sysarg )
	local sVal = Actor.getStaticVar(sysarg)
	if not sVal.drawADAwardStates then
		sVal.drawADAwardStates = {}
		return false
	end
	
	local openDay = System.getDaysSinceOpenServer()
	if sVal.drawADAwardStates[openDay] == 1 then
		return true
	end
	return false
end

function  SendADPageAwardsDrawInfo( sysarg )
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sOpenServerAdvertisementPageAward)
	--print("LogicCmd.activitySys.sysId= "..LogicCmd.activitySys.sysId.." sOpenServerAdvertisementPageAward= "..LogicCmd.activitySys.sub.sOpenServerAdvertisementPageAward)
	if (pack == nil) then
		return
	end
	local count = #OpenServerAdvertisementPageCfg
	DataPack.writeByte(pack, count) --开服配置数量

	local sVal = Actor.getStaticVar(sysarg)
	if not sVal.drawADAwardStates then
		sVal.drawADAwardStates = {}
	end
	for day=1, count do
		local state = sVal.drawADAwardStates[day] or 0
		--print("drawADAwardState= "..state)
		DataPack.writeByte(pack, state)
	end
	DataPack.flush(pack)
end


--[[
领取请求数据入口
]]
function OpenServerADAwardsDrawHandle( sysarg )
	--print("OpenServerADAwardsDrawHandle .. ")
	if IsHasDrawADtPageAwards(sysarg) then  --检查是否已经领取奖励
		SendADPageAwardsDrawInfo( sysarg )
		return
	end

	local openDay = System.getDaysSinceOpenServer()
	local awards =  OpenServerAdvertisementPageCfg[openDay].Awards
	if not awards then
		return
	end

	--向玩家发送奖励
	local actorId     = Actor.getActorId( sysarg )
	if Awards.Give( sysarg, awards,  LogIds.OpenServerADPageLogId, Lang.LogTips.OpenServerADPage ) then
		SetADAwardsDrawState( sysarg, false )
	end
	SendADPageAwardsDrawInfo( sysarg )
end

--[[
设置奖励领取状态（只设置当天）
isInit: 是否初始化当天状态
]]
function SetADAwardsDrawState( sysarg, isInit )
	local sVal = Actor.getStaticVar(sysarg)
	if not sVal.drawADAwardStates then
		sVal.drawADAwardStates = {}
	end
	local openDay = System.getDaysSinceOpenServer()
	sVal.drawADAwardStates[openDay] = isInit and 0 or 1
end

--[[
初始化文件领取状态记录（在NewDayArriveHandler中OnPlayerCrossDay(sysarg)调用）
初始化当天的领取记录
]]
function InitADAwardsDrawState( sysarg )
	SetADAwardsDrawState( sysarg, true )
end

